package assignment.ton.gui;
//import SystemState;
import assignment.ton.jenerator.Jenerator;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import assignment.ton.def.Action;
import assignment.ton.def.Class;
import assignment.ton.def.History;
import assignment.ton.def.Util;
/**
 * Responsible for creating all sub panels, managing the actions, and providin references to sub objects
 * @author Tim
 */
public class MainFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	private DrawPanel draw = new DrawPanel(this); //this is sub panel of mainPanel
	private MenuPanel menu = new MenuPanel(this); //sub panel of  leftPanel
	private MenuBar menub = new MenuBar(this); //this goes north in main frame
	private Action action;
	private History hist = new History(this);
	private JPanel mainPanel, leftPanel; //mainPanel contains leftPanel and drawPanel
	private Class classSelected = null;
	private Action previousAction = null;
	private Action previous2Action = null;
	final JFileChooser fc = new JFileChooser();
	
	public MainFrame(){
		mainPanel = new JPanel();
		leftPanel = new JPanel();
		this.setIconImage(new ImageIcon(getClass().getResource("ton_icon.gif")).getImage());
		this.setTitle("TON JAVA Generator");
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS)); //left panels a boxlayout veritcally
		leftPanel.add(menu); //add the menu panel to the leftPanel first
		leftPanel.add(Box.createVerticalGlue()); //add a dynamic filler between this and the bottom
		leftPanel.setBorder(new LineBorder(Color.black, 2));
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.LINE_AXIS)); //in the main panel set horizontal box layout
		mainPanel.add(leftPanel); // add the leftPanel to the mainPanel
		mainPanel.add(draw); //then add the draw panel
		this.add(mainPanel, BorderLayout.CENTER); //add the main panel to the center of the frame
		this.add(menub, BorderLayout.NORTH); //add the menubar to the north of the frame
		this.pack(); //sizes are the created frames :)
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //make exit do nothing
		leftPanel.setBackground(Util.background());
		mainPanel.setBackground(Util.background());
		this.setBackground(Util.background());
		this.addWindowListener(new WindowAdapter(){ //now make it do something more interesting
			public void windowClosing(WindowEvent e){
					Util.quit();
			}
		});
	}
	/**
	 * this is the action to be done
	 * @param a 
	 */
	public void setAction(Action a){
		previous2Action = previousAction;
		previousAction = action;
		action = a;
		hist.changeOccured();
		menub.setTaskLabel(a);;
	}
	public Action getAction(){
		return action;
	}
	public Action getPreviousAction(){
		return previousAction;
	}
	public Action getPrevious2Action(){
		return previous2Action;
	}
	/**
	 * this is whats selected and if nothing is change special menu
	 * @param c
	 */
	public void isSelected(Class c){
		classSelected = c;
		if(c!=null){
			menu.setSelectedVisible(true);
		}else{
			menu.setSelectedVisible(false);
		}
	}
	public Class getSelected(){
		return classSelected;
	}
	public DrawPanel getDrawPanel(){
		return draw;
	}
	/**
	 * Method for exporting
	 * First it asks a user which folder to save into then,
	 * it goes through each class and generates the java for it,
	 * Then it writes each of these classes to a java file.
	 */
	public void export(){
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int returnVal = fc.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			if(!file.canWrite()){ 
				Util.showError(this, "Cannot write to folder");
				return;
			}
			for(Class c: draw.getClasses()){
				File child = new File(file, c.getName()+".java");
				FileWriter fw;
				Jenerator j = new Jenerator(c);
				String[] output = j.getClassOutput();
				try {
					fw = new FileWriter(child);
					BufferedWriter out = new BufferedWriter(fw);
					for (String s: output){
						System.out.println(s);
						out.write(s+"\n");
					}
					out.close();
				} catch (IOException e) {
					Util.showError(this, e.getMessage());
				}
			}
			Util.showInfo(this, "Exporting successful");
		}
	}
	public History getHistObj(){
		return hist;
	}
}
